package com.wx.tnsenterprise.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.tnsenterprise.mapper.HrMapper;
import com.wx.tnsenterprise.model.dto.PageDto;
import com.wx.tnsenterprise.model.po.EnterprisePo;
import com.wx.tnsenterprise.model.po.HrPo;
import com.wx.tnsenterprise.model.dto.HrDto;
import com.wx.tnsenterprise.model.dto.HrRegisterAndLoginDto;
import com.wx.tnsenterprise.model.vo.HrVo;
import com.wx.tnsenterprise.service.HrService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author 林建权
 * @version 1.0
 * @date 2020/7/28 17:55
 */
@Service
public class HrServiceImpl implements HrService {

    @Resource
    private HrMapper hrMapper;

    @Override
    public List<HrPo> findAll() {
        return hrMapper.findAll();
    }

    @Override
    public PageInfo<HrPo> findAllPageInfo(PageDto pageDto) {
        if (pageDto.getPage()==null){
            pageDto.setPage(1);
        }
        if (pageDto.getPageSize()==null){
            pageDto.setPageSize(10);
        }
        PageHelper.startPage(pageDto.getPage(),pageDto.getPageSize());
        List<HrPo> list=hrMapper.findAll();
        PageInfo<HrPo> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public HrPo findById(Integer id) {
        return hrMapper.findById(id);
    }

    public HrPo findByPhone(String phone){
        return hrMapper.findByPhone(phone);
    }

    @Override
    public int insert(HrPo hrPo) {
        return hrMapper.insert(hrPo);
    }

    @Override
    public int update(HrPo hrPo) {
        return hrMapper.update(hrPo);
    }

    @Override
    public int delete(Integer id) {
        return hrMapper.delete(id);
    }

    //获取验证码
    @Override
    public void getCaptcha(HrRegisterAndLoginDto hrRegisterAndLoginDto) {
        //发送验证码

        return ;
    }

    //hr登录注册
    @Override
    public Integer registerOrLogin(HrRegisterAndLoginDto hrRegisterAndLoginDto) {

        //验证验证码
        Boolean bool = hrRegisterAndLoginDto.getCaptcha().equals("123456");
        if (bool){
            //验证码一致
            //检查手机号是否已注册
            HrPo hrPo = hrMapper.findByPhone(hrRegisterAndLoginDto.getPhone());
            if (hrPo == null){
                //未注册
                hrPo = new HrPo();
                hrPo.setHrPhone(hrRegisterAndLoginDto.getPhone());
                hrMapper.insert(hrPo);
                return hrPo.getId();
            }
            return hrPo.getId();

        }else {
            return 0;
        }

    }

    //创建Boss名片 修改hr信息
    @Override
    public int updateByHrDto(HrDto hrDto,MultipartFile hrPic) {

        //保存头像,获得保存路径
        String picPath = saveHrPic(hrPic);
        //创建存储对象
        HrVo hrVo = new HrVo();
        //复制对象
        BeanUtils.copyProperties(hrDto,hrVo);
        //添加属性
        hrVo.setHrPic(picPath);
        //更新数据
        return hrMapper.updateByHrVo(hrVo);
    }




    //保存头像
    private String saveHrPic(MultipartFile hrPic){
        //文件根路径
        String fileRoot = "D:\\tns\\";
        //文件一级路径
        String fileFirst = "hr\\";
        //文件二级路径
        String fileSecond = "pic\\";
        //文件总路径
        String filePath = fileRoot + fileFirst +fileSecond;
        //创建文件目录
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        //获取文件后缀
        String substring = hrPic.getOriginalFilename().substring(hrPic.getOriginalFilename().lastIndexOf("."));
        //新文件名
        String filename = "hr_pic"+substring;
        //保存文件
        try {
            hrPic.transferTo(new File(filePath,filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回文件路径
        return filePath+filename;
    }


}
